package cn.gqr.tool;

import cn.gqr.entity.Largefile;
import cn.gqr.entity.User;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Component
public class Toolkit {

    /**
     * 获取当前登录的用户信息
     * **/
    public static User getUser(){
        String principal = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = JSON.parseObject(principal, User.class);
        user.setPassword(null);
        return user;
    }

    /**
     * 把上传的图片封装成Largefile对象
     * **/
    public static Largefile filesUpload(Largefile largefile,MultipartFile file) throws IOException {
        //获取UUID并转化为String对象
        String uuid = (UUID.randomUUID().toString()).replace("-","");
        //设置uuid
        largefile.setId(uuid);
        //设置文件名称
        largefile.setFilename(file.getOriginalFilename());
        //转换byte
        byte[] buf = file.getBytes();
        //设置图片内容
        largefile.setContent(buf);
        return largefile;
    }


}


